﻿Menu = function (href,title,icon) {    
    this.href = href;
    this.title = title;
    this.icon = icon;
    this.view = true;
    this.edit = true;
    this.add = true;
    this.delete = true;
}

Menu.arrs = new Array();
Menu.add = function (obj) {
    Menu.arrs.push(obj);
}

Menu.create = function () {

    var obj ;
    obj = new Menu("profile", " Thông tin", "icon-eye-open");
    Menu.add(obj);    
    obj = new Menu("changepass", " Thay Đổi Mật Khẩu", "icon-home");
    Menu.add(obj);    
}

Menu.index = 0;
Menu.Insert = function () {
    if (Menu.index >= Menu.arrs.length) {
        return;
    }
    var url = Editor.Url + "/api/menuAPI";
    var temp = Menu.arrs[Menu.index];
    var k = Menu.index + 1;
    var item = {
        name: temp.icon,
        title: temp.title,
        url: temp.href,
        level1: 1,
        ordernumber: k,
        isdelete : false
    }
    $.ajax({
        url: url,
        type: "Post",
        data: JSON.stringify(item),
        contentType: "application/json",
        cache: false,
        success: function (data) {
            Menu.index = Menu.index + 1;
            Menu.Insert();
        }
    });
}

Menu.GetbacKup = function () {
    var url = Editor.Url + "/restore/GetListBackup";
    $.ajax({
        url: url,
        type: "Post",
        contentType: "application/json",
        cache: false,
        success: function (data) {            
            $("#type_id").find('option').remove();
            var type_id = document.getElementById("type_id");
            if (type_id == null || type_id == undefined) {               
                return;
            }
            console.dir(data);
            for (var i = 0; i < data.length; i++) {
                var optionNode = document.createElement("option");
                optionNode.setAttribute("value", data[i].value);
                optionNode.text = data[i].key;
                type_id.appendChild(optionNode);
            }
        }
    });
}

Menu.Backupdatabase = function () {
    var url = Editor.Url + "/bk/Backup";
    
    $.ajax({
        url: url,
        type: "Post",        
        contentType: "application/json",
        cache: false,
        success: function (data) {
            alert(data);
        }
    });
}

Menu.Restoredatabase = function () {
    var url = Editor.Url + "/restore/Restore";
    var item = {
        filePath: $("#type_id").val()
    }
    $.ajax({
        url: url,
        type: "Post",
        contentType: "application/json",
        data: JSON.stringify(item),
        cache: false,
        success: function (data) {
            alert(data);
        }
    });
}

Menu.left = function () {
    
   
    Menu.index = 0;
   

    var objs = Editor.read(Editor.items);

    $("#leftmenu").empty();
    $("#leftmenu").append('<li class="nav-header hidden-tablet">Main</li>');    
    for (var i = 0; i < objs.length; i++) {
        var obj = objs[i];
        var url = Editor.Url + "/" + obj.name;
        var li = '<li><a class="ajax-link" href="' + url + '"><i class="' + '"></i><span class="hidden-tablet"> ' + obj.title + '</span></a></li>';
        $("#leftmenu").append(li);
    }
    Menu.InfoUserLogIn();
}


Menu.Infos = function () {
    Menu.arrs = new Array();
    Menu.create();//call ajax
    $("#leftmenu").empty();
    $("#leftmenu").append('<li class="nav-header hidden-tablet">Main</li>');
    for (var i = 0; i < Menu.arrs.length; i++) {
        var obj = Menu.arrs[i];
        var url = Editor.Url + "/" + obj.href;
        var li = '<li><a class="ajax-link" href="' + url + '"><i class="' + '"></i><span class="hidden-tablet"> ' + obj.title + '</span></a></li>';
        $("#leftmenu").append(li);
    }
    Menu.InfoUserLogIn();
}

Menu.InfoUserLogIn = function () {
    var objUser = Editor.read(Editor.login);
    var html = "";
    html = '<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">';
    html += '<i class="icon-user"></i><span class="hidden-phone"> ' + objUser.username + '</span>';
    html += '<span class="caret"></span></a>';
    html += '<ul class="dropdown-menu">';
    html += '<li><a href="profile">Thông Tin Cá Nhân</a></li>';
    if (objUser.type_id === 1) {
        html += '<li class="divider"></li>';
        html += '<li><a href="bk">Sao lưu dự liệu</a></li>';
        html += '<li class="divider"></li>';
        html += '<li><a href="Report">Báo cáo nhân viên</a></li>';
    }
    html += '<li class="divider"></li>';
    html += '<li><a href="chat">Chat</a></li>';
    html += '<li class="divider"></li>';
    html += '<li><a href="login">Thoát</a></li>';
    html += '</ul>';
    $("#infousers").html(html);
}

